package mwt.xml.waxdbforms.presentation;

import java.io.BufferedReader;
import java.io.StringWriter;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mwt.xml.waxdbforms.business.DbmsService;
import mwt.xml.waxdbforms.business.WAXDBFormsServiceFactory;
import mwt.xml.xdbforms.dbms.metadata.impl.TableMetadata;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

public class TableManager extends MappingDispatchAction {

    public ActionForward activetableinsert(ActionMapping mapping, ActionForm actionForm,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        String schemaName = (String)request.getParameter("schemaname");
        String tableName = (String)request.getParameter("tablename");

        WAXDBFormsServiceFactory factory = WAXDBFormsServiceFactory.getInstance();
        DbmsService service = factory.getDbmsService();

        List<String> schemas = service.getSchemasList();
        request.setAttribute("schemas", schemas);

        List<TableMetadata> tables = service.getTablesList(schemaName);
        request.setAttribute("tables", tables);
        
        request.setAttribute("schema", schemaName);
        request.setAttribute("tablename", tableName);

        TableMetadata tableMetaData = service.getTbaleMetadata(schemaName, tableName);
        String xformsmodel = service.getXFormsModel(tableMetaData, request.getContextPath().concat("/user/tableinsert.do"));
        String xformsui = service.getXFormsUI(tableMetaData);
        
        request.setAttribute("xformsmodel", xformsmodel);
        request.setAttribute("xformsui", xformsui);
        
        return mapping.findForward("user.tableinsert");

    }

    public ActionForward activetableupdate(ActionMapping mapping, ActionForm actionForm,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        String schemaName = (String)request.getParameter("schemaname");
        String tableName = (String)request.getParameter("tablename");
        String pkval = (String)request.getParameter("pkval");

        WAXDBFormsServiceFactory factory = WAXDBFormsServiceFactory.getInstance();
        DbmsService service = factory.getDbmsService();

        List<String> schemas = service.getSchemasList();
        request.setAttribute("schemas", schemas);

        List<TableMetadata> tables = service.getTablesList(schemaName);
        request.setAttribute("tables", tables);

        request.setAttribute("schema", schemaName);
        request.setAttribute("tablename", tableName);

        TableMetadata tableMetaData = service.getTbaleMetadata(schemaName, tableName);
        String xformsmodel = service.getXFormsModel(tableMetaData, request.getContextPath().concat("/user/tableupdate.do"),pkval);
        String xformsui = service.getXFormsUI(tableMetaData);

        request.setAttribute("xformsmodel", xformsmodel);
        request.setAttribute("xformsui", xformsui);

        return mapping.findForward("user.tableinsert");

    }

    public ActionForward showtableinfo(ActionMapping mapping, ActionForm actionForm,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        String schemaName = (String)request.getParameter("schemaname");
        String tableName = (String)request.getParameter("tablename");

        WAXDBFormsServiceFactory factory = WAXDBFormsServiceFactory.getInstance();
        DbmsService service = factory.getDbmsService();
        
        List<String> schemas = service.getSchemasList();
        request.setAttribute("schemas", schemas);

        List<TableMetadata> tables = service.getTablesList(schemaName);
        request.setAttribute("tables", tables);

        TableMetadata tableMetaData = service.getTbaleMetadata(schemaName, tableName);

        request.setAttribute("schema", schemaName);
        request.setAttribute("tablename", tableName);
        request.setAttribute("tablemtd", tableMetaData);
        
        return mapping.findForward("user.tabledetail");
    }

    public ActionForward showtabledata(ActionMapping mapping, ActionForm actionForm,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        String schemaName = (String)request.getParameter("schemaname");
        String tableName = (String)request.getParameter("tablename");

        WAXDBFormsServiceFactory factory = WAXDBFormsServiceFactory.getInstance();
        DbmsService service = factory.getDbmsService();

        List<String> schemas = service.getSchemasList();
        request.setAttribute("schemas", schemas);

        List<TableMetadata> tables = service.getTablesList(schemaName);
        request.setAttribute("tables", tables);

        TableMetadata tableMetaData = service.getTbaleMetadata(schemaName, tableName);
        request.setAttribute("tablemtd", tableMetaData);

        Set<List<String>> tableData = service.getTableData(schemaName, tableName);
        request.setAttribute("tabledata", tableData);
        request.setAttribute("schema", schemaName);
        request.setAttribute("tablename", tableName);

        return mapping.findForward("user.tabledata");
    }

    public ActionForward tableinsert(ActionMapping mapping, ActionForm actionForm,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        BufferedReader requestBR = new BufferedReader(request.getReader());
        StringWriter sw = new StringWriter();

        String line = null;
        while ((line = requestBR.readLine()) != null) {
            sw.append(line.trim());
        }
        sw.flush();

        System.out.println("####################Tableinsert####################");
        System.out.println(sw.toString());

        WAXDBFormsServiceFactory factory = WAXDBFormsServiceFactory.getInstance();
        DbmsService service = factory.getDbmsService();
        service.insertTable(sw.toString());

        sw.close();
        return null;//mapping.findForward("user.tabledata");
    }

    public ActionForward tableupdate(ActionMapping mapping, ActionForm actionForm,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        BufferedReader requestBR = new BufferedReader(request.getReader());
        StringWriter sw = new StringWriter();

        String line = null;
        while ((line = requestBR.readLine()) != null) {
            sw.append(line.trim());
        }
        sw.flush();

        System.out.println("####################Tableupdate####################");
        System.out.println(sw.toString());

        WAXDBFormsServiceFactory factory = WAXDBFormsServiceFactory.getInstance();
        DbmsService service = factory.getDbmsService();
        service.updateTable(sw.toString());

        sw.close();
        return null;//mapping.findForward("user.tabledata");
    }

    public ActionForward deleterow(ActionMapping mapping, ActionForm actionForm,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        String schemaName = (String)request.getParameter("schemaname");
        String tableName = (String)request.getParameter("tablename");
        String pkval = (String)request.getParameter("pkval");

        WAXDBFormsServiceFactory factory = WAXDBFormsServiceFactory.getInstance();
        DbmsService service = factory.getDbmsService();

        List<String> schemas = service.getSchemasList();
        request.setAttribute("schemas", schemas);

        List<TableMetadata> tables = service.getTablesList(schemaName);
        request.setAttribute("tables", tables);

        TableMetadata tableMetaData = service.getTbaleMetadata(schemaName, tableName);
        request.setAttribute("tablemtd", tableMetaData);

        service.deleteRow(tableMetaData, pkval);

        Set<List<String>> tableData = service.getTableData(schemaName, tableName);
        request.setAttribute("tabledata", tableData);
        request.setAttribute("schema", schemaName);
        request.setAttribute("tablename", tableName);

        return mapping.findForward("user.tabledata");
    }
}
